package org.ducinaltum.android.prima;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class EventPagerActivity extends FragmentActivity {

	public static final String INTENT_EXTRA_EVENT_IDS = "event_ids";

	private long[] eventIds;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        eventIds = getIntent().getLongArrayExtra(INTENT_EXTRA_EVENT_IDS);

        setContentView(R.layout.activity_event_pager);
        ViewPager viewPager = (ViewPager)findViewById(R.id.event_pager);
        viewPager.setAdapter(new EventPagerAdapter(getSupportFragmentManager()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_event_pager, menu);
        return true;
    }


    private class EventPagerAdapter extends FragmentStatePagerAdapter {

    	public EventPagerAdapter(final FragmentManager fm) {
    		super(fm);
    	}

    	@Override
    	public int getCount() {
    		return eventIds.length;
    	}

    	@Override
    	public CharSequence getPageTitle(final int index) {
    		return "." + (index + 1);
    	}

    	@Override
    	public Fragment getItem(final int index) {
    		Fragment fragment = new EventDetailsFragment();
    		Bundle bundle = new Bundle();
    		bundle.putLong(EventDetailsFragment.EVENT_ID, eventIds[index]);
    		fragment.setArguments(bundle);
    		return fragment;
    	}
    }
}
